package me.jessyan.mvparms.demo.mvp.ui.activity.shoppcard;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.example.xrecyclerview.XRecyclerView;
import com.example.xrecyclerview.refresh.SwipyRefreshLayout;
import com.example.xrecyclerview.refresh.SwipyRefreshLayoutDirection;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.shoppcard.DaggerInvalidCardComponent;
import me.jessyan.mvparms.demo.di.module.shoppcard.InvalidCardModule;
import me.jessyan.mvparms.demo.mvp.contract.shoppcard.InvalidCardContract;
import me.jessyan.mvparms.demo.mvp.presenter.shoppcard.InvalidCardPresenter;
import me.jessyan.mvparms.demo.mvp.ui.holder.FragShoppingCardHolder;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 失效卡
 */
public class InvalidCardActivity extends BaseActivity<InvalidCardPresenter> implements InvalidCardContract.View {

    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.recyclerView)
    XRecyclerView recyclerView;
    @BindView(R.id.refresh)
    SwipyRefreshLayout refresh;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerInvalidCardComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .invalidCardModule(new InvalidCardModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_invalid_card; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
            toolbar.setListener(new CustomToolbar.ToolbarListener() {
                @Override
                public void onLeftBack() {
                    killMyself();
                }

                @Override
                public void onRightBack() {

                }
            });
            initRecyclerView();
    }

    private void initRecyclerView() {


        List<String> list = new ArrayList<>();


        LinearLayoutManager lr = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(lr);
        ArmsUtils.configRecyclerView(recyclerView,lr);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.clearHeader();
        DefaultAdapter adapter=new DefaultAdapter(list,true) {
            @Override
            public BaseHolder getHolder(View v, int viewType) {
                return new FragShoppingCardHolder(v);
            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_invalid_card;
            }


        };
        recyclerView.setAdapter(adapter);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {}

            @Override
            public void onLoadMore() {

            }
        });

        refresh.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                list.add("s");
                list.add("s"); list.add("s"); list.add("s"); list.add("s"); list.add("s"); list.add("s");
                adapter.notifyDataSetChanged();
            }
        });

        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                Intent intent = new Intent(InvalidCardActivity.this,CardDetailActivity.class);
                intent.putExtra("type",CardDetailActivity.CARD_INVAILD);

                startActivity(intent);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

}
